package com.mmd.manage.service.impl;

import com.mmd.bean.PageBean;
import com.mmd.common.entity.MmdSupplier;
import com.mmd.common.entity.MmdUser;
import com.mmd.manage.dao.MmdSupplierDao;
import com.mmd.manage.dao.UserDao;
import com.mmd.manage.service.SupplierService;
import com.mmd.manage.vo.SupplierVo;
import com.mmd.utils.ObfuseTableBase64;
import com.mmd.utils.UniqueNoUtils;
import com.mmd.utils.UniqueNoUtils.UniqueNoType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class SupplierServiceImpl implements SupplierService{
	
	@Autowired
	private MmdSupplierDao mmdSupplierDao;
	@Autowired
	private UserDao userDao;

	@Override
	public PageBean<MmdSupplier> getSupplierListPage(String keyWord, int pageNo, int pageSize) {
		List<MmdSupplier> list = mmdSupplierDao.getSupplierListPage(keyWord, (pageNo - 1) * pageSize, pageSize);
		return new PageBean<>(list, mmdSupplierDao.getSupplierListPageTotal(keyWord), pageSize, pageNo);
	}

	@Override
	@Transactional
	public void addSupplier(String supplierName, String phone, String account, String pwd) {
		MmdUser user = new MmdUser();
		user.setAccount(account);
		user.setPassword(ObfuseTableBase64.encode(pwd));
		user.setUserName(supplierName);
		user.setRealName(supplierName);
		user.setPhone(phone);
		user.setLevel("supplier");
		user.setIsFree(0);
		user.setCreateTime(new Date());
		userDao.insertUser(user);
		
		MmdSupplier supplier = new MmdSupplier();
		supplier.setSupplierCode(UniqueNoUtils.next(UniqueNoType.SU));
		supplier.setSupplierName(supplierName);
		supplier.setPhone(phone);
		supplier.setAccountUid(user.getUid());
		supplier.setCreateTime(new Date());
		mmdSupplierDao.insertSupplier(supplier);
	}

	@Override
	public SupplierVo getSupplierDetail(String supplierCode) {
		return mmdSupplierDao.getSupplierDetail(supplierCode);
	}

	@Override
	public List<MmdSupplier> getSupplierList() {
		return mmdSupplierDao.getSupplierList();
	}

	@Override
	public SupplierVo getSupplierDetailByUid(long uid) {
		return mmdSupplierDao.getSupplierDetailByUid(uid);
	}

}
